#!/usr/bin/env bash

# Copyright 2020 Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -u
set -o pipefail

$ kubectl create ns health-global

# $snippet update_config_map.sh syntax="bash"
kubectl get cm istio-sidecar-injector -n istio-system -o yaml | sed -e 's/"rewriteAppHTTPProbe":false/"rewriteAppHTTPProbe":true/' | kubectl apply -f -
# $endsnippet

# $snippet globally_re-deploy.sh syntax="bash"
$ kubectl -n health-global apply -f <(istioctl kube-inject -f @samples/health-check/liveness-http-same-port.yaml@)
# $endsnippet

$ kubectl -n health-global rollout status deployment liveness-http --timeout 60s

# $snippet re-deploy_globally_check_status.sh syntax="bash" outputis="text"
$ kubectl -n health-global get pod
# $verify verifier="lineRegex"
NAME *READY *STATUS *RESTARTS *AGE
liveness-http-[0-9a-z]*-[0-9a-z]* *2/2 *Running *0 ?
# $endsnippet

# $snippet delete_globally_deploy.sh syntax="bash"
$ kubectl -n health-global delete -f <(istioctl kube-inject -f @samples/health-check/liveness-http-same-port.yaml@)
# $endsnippet

# $snippet revert_config_map.sh syntax="bash"
kubectl get cm istio-sidecar-injector -n istio-system -o yaml | sed -e 's/"rewriteAppHTTPProbe":true/"rewriteAppHTTPProbe":false/' | kubectl apply -f -
# $endsnippet
